var express = require('express')
var mysql = require('mysql')
var bodyParser = require('body-parser')
var app = express()
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({
	extended: false
}))

var connection = mysql.createConnection({
	host: "180.97.195.131",
	port: 3306,
	user: "test2",
	password: "test2",
	database: "test2"
})
app.use("/",express.static('public'))

app.get('/typelist',function(req,res){
	connection.query(`select * from listpage_type`,[],
	function(err,data){
		if(err) {
			console.log(err)
		}else{
			res.json(data)			
				
		}
	})
	
})

app.get('/listPage',function(req,res){
	connection.query(`SELECT g.id,g.img,g.title,g.unitPrice,g.oldPrice,g.createtime,g.state,g.typeid,g.stock,g.stateWord,g.volume FROM good_list g  WHERE parentid = 1 AND unitPrice != ""`,[],
	function(err,data){
		if(err) {
			console.log(err)
		}else{
			res.json(data)			
			console.log(111)			
		}
	})

})


app.post("/register", function(req, res) {
	connection.query(`INSERT INTO user_info VALUES (id, ?, ?)`, [req.body.tel, req.body.pwd], function(err, data) {
		if(err) {
			console.log(err)
		} else {
			if(data.affectedRows === 1) {
				res.json({
					msg: "注册成功"
				})
			} else {
				res.json({
					msg: "注册失败"
				})
			}
		}
	})
})
//手机号重复注册接口
app.get("/register", function(req, res) {
	connection.query(`select count(*) count from user_info where user_tel=?`, [req.query.tel], function(err, data) {
		if(err) {
			console.log(err)
		} else {
			if(data[0].count === 1) {
				console.log(data[0].count)
				res.json({
					success: false
				})
			} else {
				console.log(data[0].count)
				res.json({
					success: true
				})
			}

		}
	})

})
//登录接口
app.post('/login', function(req, res) {
	connection.query(`select * from user_info where user_tel=? and user_pwd=?`, [req.body.tel, req.body.pwd], function(err, data) {
		if(err) {
			console.log(err)
		} else {
				res.json(data)

		}
	})
})











app.listen(7777,function(){
	console.log("程序已启动，端口号为7777")
})